﻿/*
 * Author: Kasper van Voorst
 * 
 * Description: applys an mask that are stored in the masks folder (resources)
 * */

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace LibraryCS {
    public class ConvolveMask : VisionAlgorithm {
        byte[] redValues;
        byte[] greenValues;
        byte[] blueValues;
        byte[] grayValues;
        string filePath;

        public ConvolveMask(String name)
            : base(name) {
        }

        public override System.Drawing.Bitmap DoAlgorithm(Bitmap sourceImage) {

            VImage sourceim = new VImage(sourceImage);

            //read mask from file. Initialize input and output
            redValues = sourceim.RedPixels;
            greenValues = sourceim.GreenPixels;
            blueValues = sourceim.BluePixels;
            grayValues = sourceim.GrayPixels;
            int[] theMask = MaskReader.convertToMask(filePath);
            int kernelSize = (int)Math.Sqrt(theMask.Length);
            int offset = kernelSize / 2;

            byte[] outputimage = new byte[sourceim.Height * sourceim.Width];

            //apply the mask
            for (int y = offset; y < sourceImage.Height - offset; y++) {
                for (int x = offset; x < sourceImage.Width - offset; x++) {
                    int location = (y * sourceim.Width) + x;
                    int counter = 0;
                    int gray = 0;

                    //process the kernel
                    for (int q = 0 - offset; q < offset; q++) {
                        for (int p = 0 - offset; p < offset; p++) {
                            int kernelLocation = ((q + offset) * kernelSize) + (p + offset);
                            int pixelLocation = x + (sourceim.Width * (q + y)) + p;
                            gray += (grayValues[pixelLocation] * theMask[kernelLocation]);
                            counter += theMask[kernelLocation];
                        }
                    }
                    //update the pixel
                    gray = gray / counter;
                    outputimage[location] = (byte)gray;
                }
            }
            return sourceim.returnNewBitmapOutput(outputimage);
        }

        #region GUI Functions

        public override List<System.Windows.Forms.Control> createControls() {
            string[] filePaths = Directory.GetFiles("..\\..\\..\\Resources\\Masks");
            List<Control> Controls = new List<Control>();

            // Create a Label indictating the type of setting this is
            Label lb = new Label();
            lb.AutoSize = true;
            lb.Text = "Masks";
            lb.Name = "lbMasks";

            // Create a ComboBox Control to select which mask to use
            ComboBox cb = new ComboBox();
            cb.Name = "Masks";
            cb.Size = new Size(250, 25);

            for (int x = 0; x < filePaths.Length; x++) {
                cb.Items.Add(filePaths[x]);
            }


            // Add the controls to the list
            Controls.Add(cb);
            Controls.Add(lb);

            // Return the list
            return Controls;
        }

        public override void setSettings(List<object> settings) {
            filePath = (string)((ComboBox)settings[0]).SelectedItem;
            Debug.WriteLine("Path: " + filePath);
        }

        #endregion

    }
}
